package com.ruoyi.project.demo.goods.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.demo.goods.domain.Goods;
import com.ruoyi.project.demo.goods.service.IGoodsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.demo.type.service.IGoodTypeService;

/**
 * 图书Controller
 *
 * @author demo
 * @date 2020-04-13
 */
@Controller
@RequestMapping("/demo/goods")
public class GoodsController extends BaseController {
    private String prefix = "demo/goods";

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IGoodTypeService goodTypeService;

    @RequiresPermissions("demo:goods:view")
    @GetMapping()
    public String goods() {
        return prefix + "/goods";
    }

    /**
     * 查询图书列表
     */
    @RequiresPermissions("demo:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Goods goods,ModelMap mmap) {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        mmap.put("types", goodTypeService.selectGoodTypeList(null));
        return getDataTable(list);
    }

    /**
     * 导出图书列表
     */
    @RequiresPermissions("demo:goods:export")
    @Log(title = "图书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Goods goods) {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 新增图书
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {

        mmap.put("types", goodTypeService.selectGoodTypeList(null));

        return prefix + "/add";
    }

    /**
     * 新增保存图书
     */
    @RequiresPermissions("demo:goods:add")
    @Log(title = "图书", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Goods goods) {
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改图书
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Goods goods = goodsService.selectGoodsById(id);
        mmap.put("goods", goods);
        mmap.put("types", goodTypeService.selectGoodTypeList(null));
        return prefix + "/edit";
    }

    /**
     * 修改保存图书
     */
    @RequiresPermissions("demo:goods:edit")
    @Log(title = "图书", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Goods goods) {
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除图书
     */
    @RequiresPermissions("demo:goods:remove")
    @Log(title = "图书", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodsService.deleteGoodsByIds(ids));
    }
}
